package cz.uvn.orthopedy.shared;

/**
 * <p>
 * FieldVerifier validates that the data the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> package because we use it in both
 * the client code and on the server. On the client, we verify that the name is
 * valid before sending an RPC request so the user doesn't have to wait for a
 * network round trip to get feedback. On the server, we verify that the name is
 * correct to ensure that the input is correct regardless of where the RPC
 * originates.
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is note translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public final class FieldVerifier {
	
	private static final String DIAGNOSIS_CODE = "[a-zA-Z]{1}[0-9]{3}";

	private FieldVerifier() {
	}

	public static int getNumber(String number) {
		if (number == null) {
			return -1;
		}
		// !symbol.matches("^[0-9A-Z\\.]{1,10}$")
		try {
			return Integer.parseInt(number);
		} catch (NumberFormatException nfe) {
			return -1;
		}
	}

	public static boolean isDiagnosisCodeCorrect(String code) {
		return code.matches(DIAGNOSIS_CODE);
	}
}
